/** 通用工具 */
//获取url参数
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    // if (r != null)return unescape(r[2]);
    if (r != null)return decodeURI((r[2]));
    return 0;
}

function FreshPage(){
    var info = window.location;
    var protocol = info.protocol;
    var host = info.host;
    var path = info.pathname;
    var r = protocol + "//" + host + path;
    window.location.href = r;
}

if (GetQueryString('order') && GetQueryString('sort')) {
	var order = GetQueryString('order');
    var orders = $('.order');
  
    $(orders).addClass('my_sort_order').removeClass("my_sort_asc").removeClass("my_sort_desc");   

	if (GetQueryString('sort') == "asc") {
		$("th[data-order='"+order+"']").addClass("my_sort_asc").removeClass("my_sort_order").removeClass("my_sort_desc");
	} else {
		$("th[data-order='"+order+"']").addClass("my_sort_desc").removeClass("my_sort_order").removeClass("my_sort_asc");
	}
}

//替换url参数值
function changeURLArg(url,arg,arg_val){ 
       var pattern=arg+'=([^&]*)'; 
       var replaceText=arg+'='+arg_val; 
       if(url.match(pattern)){ 
           var tmp='/('+ arg+'=)([^&]*)/gi'; 
           tmp=url.replace(eval(tmp),replaceText); 
           return tmp; 
       }else{ 
           if(url.match('[\?]')){ 
               return url+'&'+replaceText; 
           }else{ 
               return url+'?'+replaceText; 
           } 
      } 
       return url+'\n'+arg+'\n'+arg_val; 
   }
   function get_sort(pre='?'){
        var orders = $('.order');
        for (var i = 0; i < orders.length; i++) {
            var order = $(orders[i]);
            // console.log(order.data('order'));
            if(order.hasClass('my_sort_desc')){
               var dt = order.data("order")
               if(dt){
                   return pre + 'order=' + dt + '&sort=desc';
               }
            }
            else if(order.hasClass('my_sort_asc')){
                var dt = order.data("order")
                if(dt){
                    return  pre + 'order=' + dt + '&sort=asc';
                }
            }
        }
        return pre + '1=1';
   }
   function order_sort(){
    $(".order").on('click',function(){
        var order = $(this).data("order");
        var sort = "desc";
        if ($(this).hasClass("my_sort_desc")) {
            // $(this).removeClass("my_sort_desc");
            // $(this).addClass("my_sort_asc");
            sort = "asc";
        } else if ($(this).hasClass("my_sort_asc")) {
            // $(this).removeClass("my_sort_asc");
            // $(this).addClass("my_sort_desc");
            sort = "desc";
        }
        var this_url = window.location.href;
        // console.log(this_url);
        if (window.location.search) {
            this_url = changeURLArg(this_url,"order",order);
            this_url = changeURLArg(this_url,"sort",sort);
            window.location.href = this_url;
        } else {
            window.location.href = window.location.href + "?order="+order+"&sort="+sort;
        }
    })
}

$(function(){
    order_sort();
})